#include <iostream>
#include "basic/basic.h"
#include "process/processor.h"

void testThreadPool(){
	boost::this_thread::sleep(boost::posix_time::milliseconds(rand() % 100));
	std::cout << "testThreadPool th:" << boost::this_thread::get_id() << std::endl;
}
void testAsyncTaskFun(int num){
	boost::this_thread::sleep(boost::posix_time::milliseconds(rand() % 100));
	std::cout << "testAsyncTaskFun num:" << num << ",th:" << boost::this_thread::get_id() << std::endl;
}
void testChan(eyas::process::chan<int>& ch){
	std::cout << "send num:1,th:" << boost::this_thread::get_id() << std::endl;
	ch << 1;
	boost::this_thread::sleep(boost::posix_time::seconds(1));
	std::cout << "send num:2,th:" << boost::this_thread::get_id() << std::endl;
	ch << 2;
	boost::this_thread::sleep(boost::posix_time::seconds(3));
	std::cout << "send num:3,th:" << boost::this_thread::get_id() << std::endl;
	ch << 3;
}
class testAsyncTask{
public:
	void test(int num){
		boost::this_thread::sleep(boost::posix_time::milliseconds(rand() % 100));
		std::cout << "testAsyncTask::test num:" << num << ",th:" << boost::this_thread::get_id() << std::endl;
	}
};
void testTimer(){
	std::cout << "testTimer " << boost::this_thread::get_id() << std::endl;
}

class testTimerCls{
public:
	void test(){
		std::cout << "testTimerCls::test " << boost::this_thread::get_id() << std::endl;
	}
};

template <typename T>
void test_template(T t){

}

int main() {
	clock_t clk = 0;
	clock_t clk2 = 0;
	void* mem = 0;
	{	// 测试并发编程
		eyas::process::init();
		// 测试异步任务
		std::cout << "Enter test async task" << std::endl;
		getchar();
		for(int i=0; i<100; i++){
			eyas::process::fly(&testThreadPool);
			eyas::process::fly(&testAsyncTaskFun, i);
			eyas::process::fly(&testAsyncTask::test, boost::shared_ptr<testAsyncTask>(new testAsyncTask()), i);
			eyas::process::fly([i](){
				std::cout << "async task lambdas num:" << i << std::endl;
			});
			//boost::bind(testAsyncTask::test, boost::shared_ptr<testAsyncTask>(new testAsyncTask()), i);
		}
		getchar();
		// 测试任务序列
		std::cout << "Enter test sequential task" << std::endl;
		getchar();
		{
			eyas::process::sequential_task_ptr seqTaskPtr = eyas::process::create_sequential_task();
			for(int i=0; i<100; i++){
				eyas::process::fly(seqTaskPtr, &testThreadPool);
				eyas::process::fly(seqTaskPtr, &testAsyncTaskFun, i);
				eyas::process::fly(seqTaskPtr, &testAsyncTask::test, boost::shared_ptr<testAsyncTask>(new testAsyncTask()), i);
			
				eyas::process::fly(seqTaskPtr, [i](){
					std::cout << "sequential task lambdas num:" << i << std::endl;
				});
			}
		}
		getchar();
	}
	std::cout << "Enter exit the test." << std::endl;
	getchar();
	return 0;
}
